package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubMemberInfoDao;
import com.game.server.db.entity.XyClubChangesQuota;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.message.proto.in.AbstractXYmClubSetplayerReq;
import com.game.server.message.proto.out.XYInformDataAlterRsp;
import com.game.server.message.proto.out.XYmClubSetplayerRsp;

public class XYmClubSetplayerReq extends AbstractXYmClubSetplayerReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null && client.checkClick(msgcode)) {
            XYmClubSetplayerRsp rsp = new XYmClubSetplayerRsp();
            rsp.setIsok(1);
            rsp.setPlayerid(this.playerid);
            rsp.setType(this.type);
            rsp.setValue(this.value);
            rsp.setCid(this.cid);
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.cid);
            if (clubInfo != null) {
                XyClubMemberInfo clubMemberInfo = clubInfo.getPlayer().get(client.getUserId());
                XyClubMemberInfo toClubMember = clubInfo.getPlayer().get(this.playerid);
                if (clubMemberInfo != null && toClubMember != null) {
                    switch (this.type) {///1.设置管理员 2.取消管理员 3.移除成员 4.修改信誉额度
                        case 1:
                            if (clubMemberInfo.getIsManager() == 1 && toClubMember.getIsManager() == 3) {
                                int updateIsManagerCount = XyClubMemberInfoDao.updateIsManager(clubInfo.getId(), toClubMember.getUid(), 2);
                                if (updateIsManagerCount > 0) {
                                    rsp.setIsok(0);
                                    toClubMember.setIsManager(2);
                                }
                            }
                            break;
                        case 2:
                            if (clubMemberInfo.getIsManager() == 1 && toClubMember.getIsManager() == 2) {
                                int updateIsManagerCount = XyClubMemberInfoDao.updateIsManager(clubInfo.getId(), toClubMember.getUid(), 3);
                                if (updateIsManagerCount > 0) {
                                    rsp.setIsok(0);
                                    toClubMember.setIsManager(3);
                                }
                            }
                            break;
                        case 3:
                            if (clubMemberInfo.getIsManager() < toClubMember.getIsManager()) { // 被删除成员等级必须比 自身等级低
                                //通知被移除玩家

                                int deleteClubMemberCount = XyClubMemberInfoDao.deleteClubMember(toClubMember.getId(), 1);
                                if (deleteClubMemberCount > 0) {
                                    rsp.setIsok(0);
                                    toClubMember.setDel_type(1);
                                    DataManager.getInstance().getMapUserData().get(toClubMember.getUid()).getClubids().remove(Integer.valueOf(clubInfo.getId()));
                                    LogicClient toClient = (LogicClient) LogicServer.getInstance().getClient(toClubMember.getUid());

                                    if (toClient!=null && toClient.getTableid()==0){
                                        if (toClient!=null && (toClient.getClubid()>0 && toClient.getTableid()==0)){
                                            XYInformDataAlterRsp alterRsp = new XYInformDataAlterRsp();
                                            alterRsp.setType(5);
                                            alterRsp.setOperateclubid(clubMemberInfo.getUid());
                                            alterRsp.setClubid(clubInfo.getId());
                                            alterRsp.setUnionid(toClubMember.getUid());
                                            toClient.Send(alterRsp);
                                        }
                                    }
                                }
                            }
                            break;
                        case 4:
                            break;
                        default:
                            break;
//                            if (clubMemberInfo.getIsManager() < toClubMember.getIsManager()) { // 被操作成员等级必须比 自身等级低
//                                int quotaType = this.value < 0 ? 2 : 1;
//                                XyClubChangesQuota quota = new XyClubChangesQuota();
//                                quota.setCid(clubInfo.getId());
//                                quota.setMe_uid(clubMemberInfo.getUid());
//                                quota.setTo_uid(toClubMember.getUid());
//                                quota.setType(quotaType);
//                                quota.setQuota(this.value);
//                                if (quotaType == 2) {
//                                    if (toClubMember.getXingyuvalue() >= Math.abs(this.value)) {
//                                        toClubMember.setXingyuvalue(toClubMember.getXingyuvalue() + this.value);
//                                    }
//                                } else {
//                                    toClubMember.setXingyuvalue(toClubMember.getXingyuvalue() + this.value);
//                                }
//                                XyClubMemberInfoDao.updateXingyuvalue(toClubMember);
//                                rsp.setIsok(0);
//                            }
                    }
                }
            }
            client.Send(rsp);
        }
    }

}
